#!/usr/bin/env python

# Invivo AI challenge simulation environment
# Copyright 2009 Simon Funk, Alejandro Dubrovsky
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys, os
from Utils import importObject

import Config
import Model as m

from SimB2  import WorldSim
from ViewPg import WorldView


def main(args):

	from optparse import OptionParser

	parser = OptionParser("%prog [options]")
	parser.add_option("-c", "--controller", dest="controller", default=Config.defaultController, help="Controller (default: %default)")	
	parser.add_option("-s", "--save-flash", dest="saveFlash", default=None, help="Filename where to save flash animation")
	parser.add_option("-g", "--game", dest="game", default=Config.defaultGame, help="Game (default: %default)")
	options, rest = parser.parse_args(args)

	if rest:
		print "Unknown options: %s"%(rest,)
		print "Use -h to print help."
		sys.exit(1)

	Game       = importObject(options.game      , "Game")
	Controller = importObject(options.controller, "Controller")

	game = Game(Controller)
	view = WorldView(game.world)
	sim  = WorldSim(game.world)

	if options.saveFlash:
		from ViewFlash import FlashMovie		
		view2 = FlashMovie(game.world)

	sim.run()

	if options.saveFlash:
		view2.save(os.path.expanduser(options.saveFlash))


if __name__ == '__main__':

	try:
		import psyco
		psyco.full()
	except ImportError:
		print 'Note: Install Psyco to make this run faster.'
	
	sys.exit(main(sys.argv[1:]))

